﻿/*
 * Created by SharpDevelop.
 * User: Andrew Sadler
 * Date: 03/09/2012
 * Time: 12:09
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using RRRSRoguelike;
using RRRSRoguelike.Enums;
using RRRSRoguelike.Managers;
using RRRSRoguelike.Helpers.Visualizer;

namespace RRRSRoguelike.Helpers
{
	/// <summary>
	/// Description of Menu.
	/// </summary>
	public class Menu
	{
		List<MenuItem> MenuItems = new List<MenuItem>();
		int selected;
		int longest;
	
		public int Selected
		{ 
			get{return selected; }
		}
		
		public Menu()
		{
			
		}
	
		public void Add(MenuItem menuItem)
		{
			MenuItems.Add(menuItem);
	
			ArrangeMenuItems();
		}
	
		public void Clear()
		{
			MenuItems.Clear();
		}
		
		public void ArrangeMenuItems()
		{	
			longest = 0;
	
			foreach (MenuItem mi in MenuItems)
			{
				if (mi.Text.Length > longest)
					longest = mi.Text.Length;
			}
	
		}
	
		public void DrawMenu(Panel panel)
		{
			int i = 1;
			ConsoleColor cC = ConsoleColor.White;
			StringBuilder sb = new StringBuilder();
			foreach (MenuItem mi in MenuItems)
			{
				if (i-1==selected)
					cC = ConsoleColor.Cyan;
				else
					cC = ConsoleColor.White;
				sb.Clear();
				sb.Append(i.ToString() + ". ");
				sb.Append(mi.Text);
				panel.Write(sb.ToString(),i-1, cC,TextAlignment.Single);
				
				i++;
			}
			
			sb.Append("Current Selection: "+(selected +1).ToString());
			
			
		}
	
		private long lastCall;
		private long delay = 16; 
		public bool HandleInput()
		{
			bool change = false;
			
			if(GameManager.Instance.Time.ElapsedMilliseconds>lastCall+delay)
			{
				lastCall = GameManager.Instance.Time.ElapsedMilliseconds;
			
			GameManager.Instance.InputManager.ReadKeys();
			
			if(GameManager.Instance.InputManager.KeyPush(ConsoleKey.UpArrow))
			{
				WrapSelected(-1);
				change = true;
			}
			else if (GameManager.Instance.InputManager.KeyPush(ConsoleKey.DownArrow))
			{
				WrapSelected(1);
				change = true;
			}
			else if (GameManager.Instance.InputManager.KeyPush(ConsoleKey.Enter))
			{
				MenuItems[selected].Select();
				change = true;
			}
			else if (GameManager.Instance.InputManager.KeyPush(ConsoleKey.Escape))
			{
				change = true;
			}
			switch(GameManager.Instance.InputManager.currentKey.Key )
			{
				case ConsoleKey.D1:
				case ConsoleKey.NumPad1:
						SelectMenuItem(1);
						change = true;
						break;
				case ConsoleKey.D2: 
				case ConsoleKey.NumPad2:
						SelectMenuItem(2);
						change = true;
						break;
				case ConsoleKey.D3: 
				case ConsoleKey.NumPad3:
						SelectMenuItem(3);
						change = true;
						break;
				case ConsoleKey.D4: 
				case ConsoleKey.NumPad4:
						SelectMenuItem(4);
						change = true;
						break;
				case ConsoleKey.D5: 
				case ConsoleKey.NumPad5:
						SelectMenuItem(5);
						change = true;
						break;
				case ConsoleKey.D6: 
				case ConsoleKey.NumPad6:
						SelectMenuItem(6);
						change = true;
						break;
				case ConsoleKey.D7: 
				case ConsoleKey.NumPad7:
						SelectMenuItem(7);
						change = true;
						break;
				case ConsoleKey.D8: 
				case ConsoleKey.NumPad8:
						SelectMenuItem(8);
						change = true;
						break;
				case ConsoleKey.D9: 
				case ConsoleKey.NumPad9:
						SelectMenuItem(9);
						change = true;
						break;
				case ConsoleKey.D0: 
				case ConsoleKey.NumPad0:
						SelectMenuItem(10);
						change = true;
						break;
				case ConsoleKey.Escape:
						SelectMenuItem(MenuItems.Count);
						break;
			}
			}
			return change;
		}
	
		/// <summary>
		/// 
		/// </summary>
		/// <param name="index">As displayed. 1 is first.</param>
		public void SelectMenuItem(int index)
		{
	
			if (index >0 && index <= MenuItems.Count)
			{
				selected = index-1;
				MenuItems[index-1].Select();
			}
			
		}
		
		public void WrapSelected(int change)
		{
			selected += change;
			if (selected >= MenuItems.Count) selected = 0;
			else if (selected < 0) selected = MenuItems.Count-1;
		}
	}
}
